import 'package:flutter/animation.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        theme: ThemeData(
          useMaterial3: false,
          primarySwatch: Colors.blue,
        ),
        home: const MyHome()
    );
  }
}

class MyHome extends StatefulWidget {
  const MyHome({super.key});

  @override
  State<MyHome> createState() => _MyHomeState();
}

class _MyHomeState extends State<MyHome>  with SingleTickerProviderStateMixin{

  late AnimationController _animationController;

  @override
  void initState() {
    super.initState();
    _animationController = AnimationController(
      //vsync:this,让程序与手机的刷新率保持一致
      vsync: this,
      duration: const Duration(milliseconds: 2000),
      lowerBound: 0.5,    //指定动画变化范围,最低从0.2倍效果开始
      upperBound: 1.2,      //指定动画变化范围,最高为1
    );

    //设置监听器,可以监听动画效果的进度
    _animationController.addListener(() {
      print(_animationController.value);
    });
  }

  @override
  Widget build(BuildContext context) {
    return  Scaffold(
      appBar: AppBar(
        title: const Text("Flutter App"),
      ),
      body:  Center(
        child: Column(
          children: [
            const SizedBox(height: 40,),
            ScaleTransition(
              scale: _animationController,
              child: Container(
                width: 200,
                height: 200,
                color: Colors.red,
              ),
            ),
            const SizedBox(height: 40,),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                ElevatedButton(onPressed: (){
                  _animationController.forward();
                }, child: const Text("forward")),
                ElevatedButton(onPressed: (){
                  _animationController.reverse();
                }, child: const Text("reverse")),
                ElevatedButton(onPressed: (){
                  _animationController.repeat(reverse: true);
                }, child: const Text("repeat")),
                ElevatedButton(onPressed: (){
                  _animationController.stop();
                }, child: const Text("stop")),
                ElevatedButton(onPressed: (){
                  _animationController.reset();
                }, child: const Text("reset")),
              ],
            )
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          _animationController.reset();
        },
        child: const Icon(Icons.refresh),
      ),
    );
  }
}

